package com.example.fangyimanagement;

import com.example.fangyimanagement.core.LoginUser;
import com.example.fangyimanagement.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;


@RestController
@RequestMapping("/")
public class IndexController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
//    后端管理系统的登录
    @RequestMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody Map<String, String> param) {
        String account = param.get("account");
        if (account == null || account.isEmpty()) {
            return Result.failed("账号不能为空");
        }
        String password = param.get("password");
        if (password == null || password.isEmpty()) {
            return Result.failed("密码不能为空");
        }
        List<Map<String, Object>> userList = this.jdbcTemplate.queryForList("SELECT * FROM glpt_user WHERE account=?", account);
        if (userList.isEmpty() || !userList.get(0).get("password").equals(password)) {
            return Result.failed("账号或密码错误，请重试");
        }
        String token = UUID.randomUUID().toString().replace("-", "");
        String sql = "update glpt_user set token=?, token_create_time=CURRENT_TIME where account=?";
        int rows = this.jdbcTemplate.update(sql, token, account);
        if (rows > 0) {
            String querySql = "SELECT account, name, token, role_code, org_code FROM glpt_user WHERE account=?";
            List<Map<String, Object>> userList1 = this.jdbcTemplate.queryForList(querySql, account);
            return Result.success(userList1.get(0));
        } else {
            return Result.failed("登录失败，请稍后重试");
        }
    }
//  移动端用户的登录
    @RequestMapping("/userLogin")
    public Result<Map<String, Object>> userLogin(@RequestBody Map<String, String> param) {
        String sfz = param.get("account");
        if (sfz == null || sfz.isEmpty()) {
            return Result.failed("身份证号码不能为空");
        }
        String password = param.get("password");
        if (password == null || password.isEmpty()) {
            return Result.failed("密码不能为空");
        }
        List<Map<String, Object>> userList = this.jdbcTemplate.queryForList("SELECT * FROM mobile_user WHERE sfz=?", sfz);
        if (userList.isEmpty() || !userList.get(0).get("password").equals(password)) {
            return Result.failed("账号或密码错误，请重试");
        }
        String token = UUID.randomUUID().toString().replace("-", "");
        String sql = "update mobile_user set token=?, token_create_time=CURRENT_TIME where sfz=?";
        int rows = this.jdbcTemplate.update(sql, token, sfz);
        if (rows > 0) {
            String querySql = "SELECT sfz, name, token FROM mobile_user WHERE sfz=?";
            List<Map<String, Object>> userList1 = this.jdbcTemplate.queryForList(querySql, sfz);
            return Result.success(userList1.get(0));
        } else {
            return Result.failed("登录失败，请稍后重试");
        }
    }
    @RequestMapping("/logout")
    public Result<Void> logout(@RequestBody Map<String, String> param) {
        String account = param.get("account");
        int rows = this.jdbcTemplate.update("update glpt_user set token = NULL, token_create_time = null where account = ?", account);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("没找到这个账号");
        }
    }

    @RequestMapping("/updatePwd")
    public Result<Void> updatePwd(@RequestBody Map<String, String> param) {
        String account = param.get("account");
        if (account == null || account.isEmpty()) {
            return Result.failed("账号不能为空");
        }
        String currentPwd = param.get("currentPwd");
        if (currentPwd == null || currentPwd.isEmpty()) {
            return Result.failed("当前密码不能为空");
        }
        String newPwd = param.get("newPwd");
        if (newPwd == null || newPwd.isEmpty()) {
            return Result.failed("请输入新密码");
        } else if (newPwd.length() < 6 || newPwd.length() > 16) {
            return Result.failed("新密码的长度不符合规则");
        }
        String reNewPwd = param.get("reNewPwd");
        if (reNewPwd == null || reNewPwd.isEmpty()) {
            return Result.failed("请确认新密码");
        } else if (reNewPwd.length() < 6 || reNewPwd.length() > 16) {
            return Result.failed("确认新密码的长度不符合规则");
        } else if (!reNewPwd.equals(newPwd)) {
            return Result.failed("新密码两次输入不一致");
        }
        List<Map<String, Object>> userList = this.jdbcTemplate.queryForList(
                "SELECT * FROM glpt_user WHERE account=?",
                account
        );
        if (userList.isEmpty() || !userList.get(0).get("password").equals(currentPwd)) {
            return Result.failed("账号或当前密码错误，请重试");
        }
//        操作数据库
        int rows = this.jdbcTemplate.update(
                "update glpt_user set password = ? where account = ?",
                newPwd, account);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("没找到这个账号");
        }
    }

    /**
     * 字典表查询
     * @param param
     * @return
     */
    @RequestMapping("/dictList")
    public Result<List<Map<String, Object>>> dictList(@RequestBody Map<String, String> param) {
        String type = param.get("type");
        String sql = "SELECT * FROM comm_dict WHERE type = ?";
        List<Map<String, Object>> dictList = this.jdbcTemplate.queryForList(sql, type);
        return Result.success(dictList);
    }
    /**
     * 查询当前登录用户的角色
     * @param param
     * @return
     */
    @RequestMapping("/queryLoginuserRole")
    public Result<List<Map<String, Object>>> queryLoginuserRole(@RequestBody Map<String, String> param, LoginUser loginUser) {
        String manager = loginUser.getAccount();
        // 查询数据库，看当前用户角色是什么，有没有权限操作
        List<Map<String, Object>> hsjcjgList = this.jdbcTemplate.queryForList("SELECT name, account, role_code, org_code FROM glpt_user WHERE account=?", manager);
        return Result.success(hsjcjgList);
    }

    /**
     * 根据传过来的token查询是否在登录有效期
     * @param param
     * @param loginUser
     * @return
     */
    @RequestMapping("/judgeTokenIsValid")
    public Result<List<Map<String, Object>>> judgeTokenIsValid(@RequestBody Map<String, String> param, LoginUser loginUser) {
//        String manager = loginUser.getAccount();
        String token = param.get("token");
        // 查询数据库，看当前用户角色是什么，有没有权限操作
        List<Map<String, Object>> hsjcjgList = this.jdbcTemplate.queryForList("SELECT name, account, role_code, org_code FROM glpt_user WHERE token=?", token);
        return Result.success(hsjcjgList);
    }

}
